﻿using Mimas.Core.Current;
using Mimas.Framework.Shared.Data.DataTransferObjects;
using Mimas.Framework.Shared.Data.EnumObjects;
using System;
using System.Linq;

namespace Mimas.Framework.Server.DomainObjects.Mapping
{
    public static class PersonContactMapping
    {
        public static PersonContact Map(PersonContactDTO data)
        {
            PersonContact newItem = new PersonContact();

            newItem.ContactTypeId = (int)data.ContactTypeId;
            newItem.Id = data.Id;
            newItem.IsDefault = data.IsDefault;
            newItem.PersonId = data.PersonId;
            newItem.Text = data.Text;

            if (data.Id == 0)
            {
                newItem.CreatedBy = UserInfo.CurrentUser.UserId;
                newItem.CreatedOn = DateTime.Now;
            }
            else
            {
                newItem.ModifiedBy = UserInfo.CurrentUser.UserId;
                newItem.ModifiedOn = DateTime.Now;
            }

            return newItem;
        }

        public static PersonContactDTO Map(PersonContact data)
        {
            PersonContactDTO newItem = new PersonContactDTO();

            newItem.ContactTypeId = (EContactType)data.ContactTypeId;
            newItem.Id = data.Id;
            newItem.IsDefault = data.IsDefault;
            newItem.PersonId = data.PersonId;
            newItem.Text = data.Text;

            return newItem;
        }

        public static PersonContact[] Map(PersonContactDTO[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }

        public static PersonContactDTO[] Map(PersonContact[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }
    }
}
